*** Table 9 Panel A *** 
global Section4 "D:\Dropbox\unequal_gains\QJE revision plan\analysis\section4_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

use "$Section4/markup_data_final", clear
egen store_id_year=group(store_id year)

gen outlier=0
foreach i in log_change_avg_unit_price log_change_avg_unit_cost log_change_avg_markup  consumer_income_salesw {
sum `i' [aw=log(1+avg_spending_weights)], d
replace outlier=1 if (`i'>r(p99) | `i'<r(p1))
}
tab outlier

replace consumer_income_salesw=consumer_income_salesw/10000

* Set table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="N" if [_n]==3
replace Description="N clusters" if [_n]==4

egen store_id_dept_year=group(store_id department_code year)
drop if missing(store_id_dept_year) | missing(store_id_year)

* Col 1
areg log_change_avg_unit_price consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_year) cluster(store_id)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(N) if Description=="N"
replace Col_1=e(N_clust) if Description=="N clusters"

* Col 2
areg log_change_avg_unit_price consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_year) cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(N) if Description=="N"
replace Col_2=e(N_clust) if Description=="N clusters"

* Col 3
areg log_change_avg_unit_price consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_dept_year) cluster(store_id)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(N) if Description=="N"
replace Col_3=e(N_clust) if Description=="N clusters"

* Col 4
areg log_change_avg_markup consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_year) cluster(store_id)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(N) if Description=="N"
replace Col_4=e(N_clust) if Description=="N clusters"

* Col 5
areg log_change_avg_markup consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_year) cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(N) if Description=="N"
replace Col_5=e(N_clust) if Description=="N clusters"

* Col 6 
areg log_change_avg_markup consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_dept_year) cluster(store_id)
gen Col_6=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_6=`b' if Description=="Beta"
replace Col_6=`se' if Description=="SE"
replace Col_6=e(N) if Description=="N"
replace Col_6=e(N_clust) if Description=="N clusters"

* Col 7 
areg log_change_avg_unit_cost consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_year) cluster(store_id)
gen Col_7=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_7=`b' if Description=="Beta"
replace Col_7=`se' if Description=="SE"
replace Col_7=e(N) if Description=="N"
replace Col_7=e(N_clust) if Description=="N clusters"

* Col 8 
areg log_change_avg_unit_cost consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_year) cluster(product_module_code)
gen Col_8=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_8=`b' if Description=="Beta"
replace Col_8=`se' if Description=="SE"
replace Col_8=e(N) if Description=="N"
replace Col_8=e(N_clust) if Description=="N clusters"

* Col 9 
areg log_change_avg_unit_cost consumer_income_salesw [aw=sqrt(avg_spending_weights)] if trimmed==0 & outlier==0, absorb(store_id_dept_year) cluster(store_id)
gen Col_9=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_9=`b' if Description=="Beta"
replace Col_9=`se' if Description=="SE"
replace Col_9=e(N) if Description=="N"
replace Col_9=e(N_clust) if Description=="N clusters"

order Descr Col*
keep Descr Col* 
drop if [_n]>4
format Col* %15.3f
save "$resultspath/Table8a", replace
